<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\AdminRecorder;

class AdminController extends BaseController{

    public function actionAdd(){
        $post = \Yii::$app->request->post();
        $model = AdminRecorder::find()->where('admin_name=:name',[':name' => $post['AdminRecorder']['admin_name']])->one();
        if($model){
            $result = ['result'=>0,'data'=>'已经存在该管理员，请更改'];
            return json_encode($result);
        }
        $model = new AdminRecorder();
        $result = $model->add($post);
        return json_encode($result);
    }


    public function actionList(){
        $list = AdminRecorder::find()->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] =date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['updatetime'] = date('Y-m-d H:i:s',$v['updatetime']);
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = AdminRecorder::find()->select(['admin_name','id'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionSaveone(){
        $post = \Yii::$app->request->post();
        $id = $post['AdminRecorder']['id'];
        $model = AdminRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $admin_password = $post['AdminRecorder']['admin_password'];
        $admin_password2 = $post['AdminRecorder']['admin_password2'];
        if(!empty($admin_password)||!empty($admin_password2)){
            if($admin_password!==$admin_password2){
                $result = ['result'=>0,'data'=>'两次输入密码不一样，请重新操作'];
                return json_encode($result);
            }
            $model->admin_password=\Yii::$app->security->generatePasswordHash($admin_password);
            $model->save(false);
        }
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = AdminRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

}
